<template>
  <c-dialog ref="dialog" width="70%" :open-fun="openFun" title="申请延期" @close="closeHandle">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="90px">
      <c-list-item label="调整后预计进场时间" prop="afterAdjustTime">
        <el-date-picker
          v-model="formData.afterAdjustTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期"
          clearable
        />
      </c-list-item>
      <c-list-item label="延期原因" prop="delayReason">
        <el-input v-model="formData.delayReason" type="textarea" maxlength="1024" show-word-limit style="width:100%" :autosize="{minRows:3}" placeholder="延期原因" clearable />
      </c-list-item>
      <c-list-item label="附件上传">
        <!-- <c-section title="附件上传"> -->
        <c-file-table-box v-model="files" type-code="ACCESS143" :table-height="100" belong-model="SHOP_DECORATION" :model-id="id" />
        <!-- </c-section> -->
      </c-list-item>

      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { putDelayIn } from '@/api/decorate'
export default {
  name: 'Postpone',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      files: [],
      id: '',
      formData: {
        afterAdjustTime: '',
        delayReason: ''
      },
      formRules: {
        afterAdjustTime: [{ required: true, message: '请选择调整后预计进场时间', trigger: 'change' }],
        delayReason: [{ required: true, message: '请输入延期原因', trigger: 'change' }]
      }
    }
  },
  methods: {
    openFun() {
      if (!this.singleJudge(this.selData, '申请延期', true)) {
        return false
      }
      const d = this.selData[0]
      this.id = d.id
      // if (d.dataFrom !== 'SLM') {
      //   this.$message.warning('系统生成的数据才能申请延期')
      //   return false
      // }
      return true
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        return this.$gsec('EXCEPT011').then(([cfg, ecd]) => {
          const d = this.selData[0]
          ecd.constCode = d.constCode
          ecd.bizCode = d.decorationTaskCode
          ecd.modelId = d.id
          ecd.exceptionDesc = this.formData.delayReason
          const t = this.$gsed()
          t.beforeAdjustTime = d.expectApplyReportTime || ''
          t.afterAdjustTime = (this.formData.afterAdjustTime || '').replace(/ \d{2}:\d{2}:\d{2}/, ' 23:59:59')
          t.exceptionDesc = this.formData.delayReason
          // t.belongDuty = d.belongDuty || ''
          t.remark = ''
          // =========
          // t.cutRegionArea = 0
          // t.expectRestoreTime = '2021-07-31 14:28:00'
          // t.delayDay = 2
          // t.expectEndTime = '2021-07-31 14:28:00'
          // t.isSampleChange = 1
          // ==========
          ecd.shopExceptionDetailDTOS = [t]

          const fileList = this.files.map(el => { return el.response.data })
          // const temp = fileList.filter(v => !v.dr)
          // if (temp.length === 0) {
          //   this.$message.warning('请上传附件')
          //   return
          // }
          this.$set(ecd, 'shopAttachmentDTOS', fileList || [])
          const data = {
            exceptionDTO: ecd,
            id: d.id
            // ...this.formData
          }

          return putDelayIn(data).then(res => {
            this.$message.success('操作成功！')
            this.$refs.dialog.visible = false
            this.$emit('on-success')
          })
        })
      })
    },
    cancelHandle() {
      this.$refs.form.resetFields()
      this.$refs.dialog.visible = false
    },
    closeHandle() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
